<html>
<head><meta charset="utf-8"><title>peephole optimization · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html">peephole optimization</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211676750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211676750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211676750">(Sep 29 2020 at 20:04)</a>:</h4>
<p>Hey folks -- <span class="user-mention" data-user-id="220917">@Nick Fitzgerald</span> was talking to me about the idea of doing peephole optimizations in Mir. I see this was discussed before in <a class="stream-topic" data-stream-id="189540" href="/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/peephole.20opts.20in.20mir">#t-compiler/wg-mir-opt &gt; peephole opts in mir</a>. I thought I'd kind of cc <span class="user-mention" data-user-id="125250">@Wesley Wiser</span> and <span class="user-mention" data-user-id="124288">@oli</span> and whomever else is here and let you all talk out what work there is to be done :)</p>



<a name="211682324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211682324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211682324">(Sep 29 2020 at 20:50)</a>:</h4>
<p>We do have an <a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/transform/instcombine.rs">InstCombine</a> pass which is basically just a set of peephole optimizations. I think we actually landed some new ones recently: <a href="https://github.com/rust-lang/rust/issues/76067">#76067</a> <a href="https://github.com/rust-lang/rust/issues/76683">#76683</a></p>



<a name="211682360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211682360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211682360">(Sep 29 2020 at 20:50)</a>:</h4>
<p>Writing them in the current form is a bit tedious though</p>



<a name="211685790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211685790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211685790">(Sep 29 2020 at 21:18)</a>:</h4>
<p>nice!</p>
<p>my thinking was that it might be possible to integrate <a href="https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/peepmatic/README.md">Peepmatic</a> into rustc and to work on mir since it is already generic over the IR that it is operating upon</p>
<p>it would require a simple reaching defs analysis to determine whether two uses of a value are the same or not tho</p>
<p>this would potentially make it easier to write peepholes and also make it cheap to have many peepholes since they are combined into a compact transducer automaton that shares not just prefixes but also suffixes of pattern matches and the associated RHSes</p>



<a name="211685976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211685976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211685976">(Sep 29 2020 at 21:19)</a>:</h4>
<p>That would be amazing!</p>



<a name="211686456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211686456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211686456">(Sep 29 2020 at 21:24)</a>:</h4>
<p>Is the resulting automation something we can save &amp; load at rustc runtime or do we have to recompile it every time rustc is invoked?</p>



<a name="211686585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211686585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211686585">(Sep 29 2020 at 21:25)</a>:</h4>
<p>integrating peepmatic basically involves implementing these traits for the IR:</p>
<ul>
<li><a href="https://docs.rs/peepmatic-runtime/0.2.0/peepmatic_runtime/instruction_set/trait.InstructionSet.html">https://docs.rs/peepmatic-runtime/0.2.0/peepmatic_runtime/instruction_set/trait.InstructionSet.html</a></li>
<li><a href="https://docs.rs/peepmatic-traits/0.67.0/peepmatic_traits/trait.TypingRules.html">https://docs.rs/peepmatic-traits/0.67.0/peepmatic_traits/trait.TypingRules.html</a></li>
</ul>



<a name="211686675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211686675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211686675">(Sep 29 2020 at 21:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125250">Wesley Wiser</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/peephole.20optimization/near/211686456">said</a>:</p>
<blockquote>
<p>Is the resulting automation something we can save &amp; load at rustc runtime or do we have to recompile it every time rustc is invoked?</p>
</blockquote>
<p>you compile it once and only need to recompile it whenever updating the set of peepholes (or peepmatic itself)</p>



<a name="211686773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211686773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211686773">(Sep 29 2020 at 21:27)</a>:</h4>
<p>definitely intended that only peephole authors deal with compiling the DSL into an automata; and even most rustc hackers wouldn't need to worry about it</p>



<a name="211687005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211687005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211687005">(Sep 29 2020 at 21:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="220917">Nick Fitzgerald</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/peephole.20optimization/near/211686585">said</a>:</p>
<blockquote>
<p>integrating peepmatic basically involves implementing these traits for the IR: ...</p>
</blockquote>
<p>and here are the implementations of these things in cranelift, if you're curious what they look like:</p>
<ul>
<li><a href="https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/codegen/src/peepmatic.rs#L28">https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/codegen/src/peepmatic.rs#L28</a></li>
<li><a href="https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/codegen/src/peepmatic.rs#L625">https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/codegen/src/peepmatic.rs#L625</a></li>
</ul>



<a name="211687222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211687222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211687222">(Sep 29 2020 at 21:31)</a>:</h4>
<p>I wonder if MIR is a bit too high-level for <code>InstructionSet</code></p>



<a name="211687273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211687273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211687273">(Sep 29 2020 at 21:31)</a>:</h4>
<p>how so?</p>



<a name="211687682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211687682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211687682">(Sep 29 2020 at 21:35)</a>:</h4>
<p>Looking at the <code>make_inst_{n}</code> functions, I can kind of see how we could implement those for MIR statements but each statement uses different data types</p>



<a name="211687721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211687721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211687721">(Sep 29 2020 at 21:35)</a>:</h4>
<p>Sometimes <code>Place</code>s or <code>Rvalue</code>s or sometimes additional enums or things</p>



<a name="211687801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211687801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211687801">(Sep 29 2020 at 21:36)</a>:</h4>
<p>I guess we could wrap all of those in another enum type and then just panic if we try to construct an invalid Statement</p>



<a name="211688009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688009">(Sep 29 2020 at 21:38)</a>:</h4>
<p>ok, I think I understand; it is more about the type definitions of mir itself, rather than the level of abstraction of the IR</p>



<a name="211688046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688046">(Sep 29 2020 at 21:38)</a>:</h4>
<p>I'm not sure about other stuff like <code>instruction_result_bit_width</code>. MIR statements don't have results at all since they're side-effect-y. We could maybe return a size based whatever value was written or read or something.</p>



<a name="211688073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688073">(Sep 29 2020 at 21:39)</a>:</h4>
<p>that bit could probably be ignored; it is just used because we have limits on how wide our immediates can be in cranelift</p>



<a name="211688098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688098">(Sep 29 2020 at 21:39)</a>:</h4>
<p>Ok gotcha</p>



<a name="211688133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688133">(Sep 29 2020 at 21:39)</a>:</h4>
<p>Sorry, not trying to throw obstacles in the way, I think it's a really cool idea! <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="211688208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688208">(Sep 29 2020 at 21:40)</a>:</h4>
<p>yeah totally understand; it may not work due to subtle assumptions based on cranelift's IR, but definitely worth thinking about IMO!</p>



<a name="211688371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688371">(Sep 29 2020 at 21:41)</a>:</h4>
<p>We've talked a little before about having a different IR for optimizations. MIR was not really designed to be optimization friendly, it was really just more concerned with being nice to consume from the borrow checker (iirc).</p>



<a name="211688446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688446">(Sep 29 2020 at 21:42)</a>:</h4>
<p>MIR isn't SSA for example</p>



<a name="211688766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688766">(Sep 29 2020 at 21:45)</a>:</h4>
<p>yeah, SSA would make this a lot easier</p>



<a name="211688777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211688777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211688777">(Sep 29 2020 at 21:45)</a>:</h4>
<p>no need for the reaching defs analysis</p>



<a name="211689129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211689129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211689129">(Sep 29 2020 at 21:48)</a>:</h4>
<p>It is a really interesting idea though so I wonder if there's some concepts we can borrow (heh) from peepmatic</p>



<a name="211689217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211689217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211689217">(Sep 29 2020 at 21:49)</a>:</h4>
<p>The traits and concepts you have in peepmatic seem really well thought out, I'd hate to contort them just so we can support MIR :)</p>



<a name="211689336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211689336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211689336">(Sep 29 2020 at 21:50)</a>:</h4>
<p>Having a DSL for matching and manipulating MIR would be amazing</p>



<a name="211689420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211689420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211689420">(Sep 29 2020 at 21:51)</a>:</h4>
<p>We've started to see some of the same classes of issues pop up and perhaps we can eliminate them by baking the solutions into the DSL instead of making each pass do that work.</p>



<a name="211692750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211692750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211692750">(Sep 29 2020 at 22:24)</a>:</h4>
<p>a lot of the traits design exists just to be able to build peepmatic without building the rest of cranelift, but also not have peepmatic have its own definition of a subset of cranelift's IR (which is what it originally did, and converting between the two was showing up in profiles)</p>
<p>but yeah, big +1 on a DSL that is type checked (and eventually gets something like LLVM's Alive). even better if the DSL is bit width poly morphic, so you don't need to write (for example) <code>x + 0 --&gt; x</code> for every bit width. maybe the DSL also understands commutativity too (peepmatic doesn't but maybe it should)</p>



<a name="211692788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211692788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick Fitzgerald <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211692788">(Sep 29 2020 at 22:25)</a>:</h4>
<p>heck you might even be able to start by forking peepmatic, if it comes to that</p>



<a name="211786725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211786725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211786725">(Sep 30 2020 at 16:31)</a>:</h4>
<p>Would be amazing to have a DSL for this stuff.  Doing it in code is just so hard to see what's actually been implemented, since there's just so much code to churn through.  (Whether it's a macro or codegen I don't have an opinion.)</p>
<p>Last I looked there were some really easy peephole opportunities, like <a href="https://github.com/rust-lang/rust/pull/46440/files#diff-3dc86956f6f4f39933f8572ed8ef050aL69">https://github.com/rust-lang/rust/pull/46440/files#diff-3dc86956f6f4f39933f8572ed8ef050aL69</a> (wow, that was almost 3 years ago).  Though IIRC the <code>StorageLive/Dead</code> made peephole more annoying than would be ideal.</p>



<a name="211787552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211787552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211787552">(Sep 30 2020 at 16:37)</a>:</h4>
<p>There are plans to eliminate the storage annotations and just compute them on the fly</p>



<a name="211872861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211872861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211872861">(Oct 01 2020 at 06:21)</a>:</h4>
<p>Note that we're removing the def-use analysis, so if you need it back, here's the link to the version just before removal: <a href="https://github.com/rust-lang/rust/blob/d92d28e523bf056ab4eb752510ec52fe4f1c6311/compiler/rustc_mir/src/util/def_use.rs">https://github.com/rust-lang/rust/blob/d92d28e523bf056ab4eb752510ec52fe4f1c6311/compiler/rustc_mir/src/util/def_use.rs</a></p>



<a name="211872868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211872868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211872868">(Oct 01 2020 at 06:21)</a>:</h4>
<p>removal happening in <a href="https://github.com/rust-lang/rust/pull/77373/files">https://github.com/rust-lang/rust/pull/77373/files</a></p>



<a name="211959185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211959185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211959185">(Oct 01 2020 at 19:09)</a>:</h4>
<p>What would you imagine the DSL looking like (roughly)? I'm just curious :)</p>



<a name="211962004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211962004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211962004">(Oct 01 2020 at 19:29)</a>:</h4>
<p>If you're talking to fitzgen, here's how peepmatic's DSL looks <a href="https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/peepmatic/README.md#example">https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/peepmatic/README.md#example</a></p>



<a name="211962168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211962168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211962168">(Oct 01 2020 at 19:30)</a>:</h4>
<p>and here's an example of GCC's <a href="https://github.com/gcc-mirror/gcc/blob/c2978b3405884e38429c1937f416753ca88d3cd6/gcc/match.pd#L5237-L5254">https://github.com/gcc-mirror/gcc/blob/c2978b3405884e38429c1937f416753ca88d3cd6/gcc/match.pd#L5237-L5254</a></p>



<a name="211964325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211964325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211964325">(Oct 01 2020 at 19:47)</a>:</h4>
<p>Thanks, I'll take a look!</p>



<a name="211964369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211964369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211964369">(Oct 01 2020 at 19:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116113">lqd</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/peephole.20optimization/near/211962168">said</a>:</p>
<blockquote>
<p>and here's an example of GCC's <a href="https://github.com/gcc-mirror/gcc/blob/c2978b3405884e38429c1937f416753ca88d3cd6/gcc/match.pd#L5237-L5254">https://github.com/gcc-mirror/gcc/blob/c2978b3405884e38429c1937f416753ca88d3cd6/gcc/match.pd#L5237-L5254</a></p>
</blockquote>
<p>Ah, it's some kind of Lisp</p>



<a name="211964587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211964587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211964587">(Oct 01 2020 at 19:49)</a>:</h4>
<p>S-expressions are easy to parse :)</p>



<a name="211964590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211964590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211964590">(Oct 01 2020 at 19:49)</a>:</h4>
<p>Based on a first look, peepmatic's DSL looks more readable</p>



<a name="211964632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/211964632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#211964632">(Oct 01 2020 at 19:49)</a>:</h4>
<p>(It's also Lisp-like <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> )</p>



<a name="213964292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20optimization/near/213964292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20optimization.html#213964292">(Oct 20 2020 at 18:56)</a>:</h4>
<p>Based on this thread, if you generate a full-fledged SSA data structure, that step might improve LLVM IR generation period:<br>
<span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/Convert.20to.20SSA.20during.20llvm.20ir.20codegen/near/213904004">said</a>:</p>
<blockquote>
<p>I noticed that cg_llvm always uses <code>alloca</code> to store locals even when it is possible to use SSA values. This leads to more inefficient code and more ir to churn through for llvm. While llvm is able to turn it into SSA using the mem2reg pass, this doesn't happen for <code>-Copt-level=0</code>, which is the default. This means that cg_llvm generates slower code in debug mode than necessary. cg_clif is able to directly turn it into SSA form on the fly using cranelift_frontend, which uses an algorithm based on "Simple and Efficient Construction of Static Single Assignment Form" (<a href="https://github.com/bytecodealliance/wasmtime/blob/76998f0404d83f1c303887c5fd207e14a07dcf4c/cranelift/frontend/src/ssa.rs">cranelift source</a>)</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">a</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<div class="codehilite" data-code-language="LLVM"><pre><span></span><code><span class="k">define</span> <span class="k">i8</span> <span class="vg">@_ZN7example1a17ha389e3fd0a4f8f11E</span><span class="p">()</span> <span class="k">unnamed_addr</span> <span class="vg">#0</span> <span class="p">{</span>
  <span class="nv">%a</span> <span class="p">=</span> <span class="k">alloca</span> <span class="k">i8</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span>
  <span class="k">store</span> <span class="k">i8</span> <span class="m">0</span><span class="p">,</span> <span class="k">i8</span><span class="p">*</span> <span class="nv">%a</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span>
  <span class="k">store</span> <span class="k">i8</span> <span class="m">1</span><span class="p">,</span> <span class="k">i8</span><span class="p">*</span> <span class="nv">%a</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span>
  <span class="nv nv-Anonymous">%0</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i8</span><span class="p">,</span> <span class="k">i8</span><span class="p">*</span> <span class="nv">%a</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span>
  <span class="k">ret</span> <span class="k">i8</span> <span class="nv nv-Anonymous">%0</span>
<span class="p">}</span>
</code></pre></div>

<div class="codehilite"><pre><span></span><code>function u0:0() -&gt; i8 system_v {
                                block0:
                                    nop
                                    jump block1

                                block1:
                                    nop
@0001                               v0 = iconst.i8 0
@0002                               v1 = iconst.i8 1
@0003                               v2 = iconst.i8 1
@0005                               return v2
}
</code></pre></div>


<p>As for the speed difference. Look at this:</p>
<div class="codehilite"><pre><span></span><code>$ ./raytracer_cg_clif
10519 milliseconds elapsed.
$ ./raytracer_cg_clif_no_ssa
19927 milliseconds elapsed.
</code></pre></div>


<p>This is both with the same sysroot compiled with SSA conversion.</p>
</blockquote>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>